home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / sprites / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-01  |  8.9 KB  |  414 lines

  1. /*
  2.     main.c
  3.  
  4.     A sample Windows application
  5.  
  6.     Another fine Herman Rodent production
  7.  
  8. */
  9.  
  10. #include "global.h"
  11.  
  12. //
  13. // local data
  14. //
  15.  
  16. static BOOL bCaptured = FALSE;
  17. static PSPRITE pCapturedSprite = NULL;
  18. static int iDX, iDY;
  19. static PSPRITE pSelectedSprite = NULL;
  20.  
  21. //
  22. // local functions
  23. //
  24.  
  25. static void Create(HWND hWnd, LPCREATESTRUCT lpCI);
  26. static void Size(HWND hWnd, UINT uiX, UINT uiY);
  27. static void Command(HWND hWnd, WPARAM wParam, LPARAM lParam);
  28. static void MeasureItem(HWND hWnd, UINT uiCtl, LPMEASUREITEMSTRUCT lpMI);
  29. static void DrawItem(HWND hWnd, UINT uiCtl, LPDRAWITEMSTRUCT lpDI);
  30. static void InitMenuPopup(HMENU hMenu, UINT uiIndex, BOOL bSystem);
  31.  
  32. //
  33. // Entry point
  34. //
  35.  
  36. int PASCAL WinMain(HINSTANCE hInstance,
  37.                    HINSTANCE hPrevInstance,
  38.                    LPSTR lpszCmdLine,
  39.                    int cmdShow)
  40. {
  41.     MSG msg;
  42.  
  43.     hAppInstance = hInstance;
  44.     szAppName = "Sprites";
  45.  
  46. #ifdef DEBUG
  47.  
  48.     //
  49.     // Get the debug level
  50.     //
  51.  
  52.     __iDebugLevel = GetProfileInt(szAppName, "debug", 1);
  53.  
  54. #endif // DEBUG
  55.  
  56.     //
  57.     // We allow multiple instances
  58.     //
  59.  
  60.     if (!hPrevInstance) {
  61.         if (!InitFirstInstance(hInstance)) {
  62.             return 1;
  63.         }
  64.     }
  65.  
  66.     //
  67.     // Do the per instance initialization
  68.     //
  69.  
  70.     if (!InitCurrentInstance(hInstance, lpszCmdLine, cmdShow)) {
  71.         return 1;
  72.     }
  73.  
  74.     //
  75.     // Check for messages from Windows and process them.
  76.     // If we have nothing else to do, maybe perform some idle function
  77.     // 
  78.  
  79.     do {
  80.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  81.  
  82.             //
  83.             // got a message to process
  84.             //
  85.  
  86.             if (msg.message == WM_QUIT) break;
  87.  
  88.             //
  89.             // Do the accelerator thing
  90.             //
  91.  
  92.             if (!TranslateAccelerator(hwndMain, hAccTable, &msg)) {
  93.                 TranslateMessage(&msg);
  94.                 DispatchMessage(&msg);
  95.             }
  96.  
  97.         } else {
  98.  
  99.             //
  100.             // perform some idle routine or just give up so Windows
  101.             // can run till our next message.
  102.             //
  103.  
  104. #ifdef WAIT_LOOP
  105.             if (bAutoUpdate && !bCaptured) {
  106.                 UpdatePositions();
  107.             } else {
  108.                 WaitMessage();
  109.             }
  110. #else
  111.             WaitMessage();
  112. #endif
  113.         }
  114.     } while (1);
  115.  
  116.     return (msg.wParam);
  117. }
  118.     
  119. //
  120. // main window message handler
  121. //
  122.  
  123. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  124. {
  125.     PAINTSTRUCT ps;
  126.  
  127.     switch(msg) {
  128.     case WM_CREATE:
  129.         Create(hWnd, (LPCREATESTRUCT)lParam);
  130.         break;
  131.  
  132.     case WM_SIZE:
  133.         Size(hWnd, LOWORD(lParam), HIWORD(lParam));
  134.         break;
  135.  
  136. #ifdef TIMER_TICKS
  137.     case WM_TIMER:
  138.         if (bAutoUpdate && !bCaptured) UpdatePositions();
  139.         break;
  140. #endif
  141.  
  142.     case WM_LBUTTONDOWN:
  143.  
  144.         dprintf3("Mouse down at %u,%u", LOWORD(lParam), HIWORD(lParam));
  145.         if (!bCaptured) {
  146.  
  147.             //
  148.             // See if it hit a sprite
  149.             //
  150.  
  151.             pCapturedSprite = SpriteHitTest(LOWORD(lParam), HIWORD(lParam));
  152.  
  153.             if (pCapturedSprite && IsSpriteSelectable(pCapturedSprite)) {
  154.  
  155.                 bCaptured = TRUE;
  156.                 SetCapture(hWnd);
  157.                 dprintf2("Sprite hit");
  158.                 iDX = LOWORD(lParam) - pCapturedSprite->x;
  159.                 iDY = HIWORD(lParam) - pCapturedSprite->y;
  160.                 dprintf3(" Sprite is at %u,%u,%u (%ux%u)", 
  161.                          pCapturedSprite->x, 
  162.                          pCapturedSprite->y, 
  163.                          pCapturedSprite->z, 
  164.                          pCapturedSprite->width,
  165.                          pCapturedSprite->height);
  166.             }
  167.         }
  168.         break;
  169.  
  170.     case WM_MOUSEMOVE:
  171.  
  172.         if (bCaptured) {
  173.  
  174.             SetSpritePosition(pCapturedSprite, 
  175.                               LOWORD(lParam)-iDX, 
  176.                               HIWORD(lParam)-iDY,
  177.                               UPDATE_SCREEN);
  178.  
  179.         }
  180.         break;
  181.  
  182.     case WM_LBUTTONUP:
  183.  
  184.         dprintf3("Mouse up at %u,%u", LOWORD(lParam), HIWORD(lParam));
  185.         if (bCaptured) {
  186.  
  187.             ReleaseCapture();
  188.             bCaptured = FALSE;
  189.             pCapturedSprite = NULL;
  190.             dprintf2("Sprite released");
  191.         }
  192.         break;
  193.  
  194.     case WM_LBUTTONDBLCLK:
  195.  
  196.         pSelectedSprite = SpriteHitTest(LOWORD(lParam), HIWORD(lParam));
  197.         if (pSelectedSprite) {
  198.             SpriteDialog(pSelectedSprite);
  199.         } else {
  200.             MessageBeep(NULL);
  201.         }
  202.         break;
  203.  
  204.  
  205.     case WM_COMMAND:
  206.         Command(hWnd, wParam, lParam); 
  207.         break;
  208.  
  209.     case WM_MEASUREITEM:
  210.         MeasureItem(hWnd, (UINT)wParam, (LPMEASUREITEMSTRUCT)lParam);
  211.         return (LRESULT) TRUE;
  212.  
  213.     case WM_DRAWITEM:
  214.         DrawItem(hWnd, (UINT)wParam, (LPDRAWITEMSTRUCT) lParam);
  215.         break;
  216.  
  217.     case WM_PAINT:
  218.         BeginPaint(hWnd, &ps);
  219.         Paint(ps.hdc, &(ps.rcPaint));
  220.         EndPaint(hWnd, &ps);
  221.         break;
  222.  
  223.     case WM_QUERYNEWPALETTE:
  224.     case WM_PALETTECHANGED:
  225.         return PaletteMessage(hWnd, msg, wParam, lParam);
  226.         break;
  227.  
  228.     case WM_INITMENUPOPUP:
  229.         InitMenuPopup((HMENU)wParam, LOWORD(lParam), (BOOL)HIWORD(lParam));
  230.         break;
  231.  
  232.     case WM_DESTROY:
  233.         Terminate();
  234.         PostQuitMessage(0);
  235.         break;
  236.  
  237.     default:
  238.         return DefWindowProc(hWnd, msg, wParam, lParam);
  239.         break;
  240.     }
  241.     return NULL;
  242. }
  243.  
  244. //
  245. // Process WM_CREATE message
  246. //
  247.  
  248. static void Create(HWND hWnd, LPCREATESTRUCT lpCI)
  249. {
  250.  
  251. }
  252.  
  253. //
  254. // Process WM_SIZE message
  255. //
  256.  
  257. static void Size(HWND hWnd, UINT uiX, UINT uiY)
  258. {
  259.  
  260. }
  261.  
  262. //
  263. // Process WM_COMMAND messages
  264. //
  265.  
  266. static void Command(HWND hWnd, WPARAM wParam, LPARAM lParam) 
  267. {
  268.     PRINTDLG pd;
  269.  
  270.     switch (wParam) {
  271.     case IDM_LOADSCENE:
  272.         LoadScene(NULL);
  273.         break;
  274.  
  275.     case IDM_LOADBKGND:
  276.         LoadBackground(NULL, UPDATE_SCREEN);
  277.         break;
  278.  
  279.     case IDM_LOADSPRITE:
  280.         LoadSprite(NULL, UPDATE_SCREEN);
  281.         break;
  282.  
  283.     case IDM_PRINT:
  284.         memset(&pd, 0, sizeof(pd));
  285.         pd.lStructSize = sizeof(pd);
  286.         pd.hwndOwner = hWnd;
  287.         pd.Flags = PD_RETURNDC;
  288.         if (PrintDlg(&pd)) {
  289.             Print(pd.hDC);
  290.         }
  291.         DeleteDC(pd.hDC);
  292.         if (pd.hDevMode) GlobalFree(pd.hDevMode);
  293.         if (pd.hDevNames) GlobalFree(pd.hDevNames);
  294.         break;
  295.  
  296.     case IDM_PRINTSETUP:
  297.         memset(&pd, 0, sizeof(pd));
  298.         pd.lStructSize = sizeof(pd);
  299.         pd.hwndOwner = hWnd;
  300.         pd.Flags = PD_PRINTSETUP;
  301.         PrintDlg(&pd);
  302.         break;
  303.  
  304.     case IDM_REFRESH:
  305.         InvalidateRect(hwndMain, NULL, FALSE);
  306.         break;
  307.  
  308.     case IDM_REDRAW:
  309.         Redraw(NULL, UPDATE_SCREEN);
  310.         break;
  311.  
  312.     case IDM_SHOWUPDATES:
  313.         bShowUpdateRects = !bShowUpdateRects;
  314.         break;
  315.  
  316.     case IDM_AUTOUPDATE:
  317.         bAutoUpdate = !bAutoUpdate;
  318.         break;
  319.  
  320.     case IDM_FILLBKGND:
  321.         FillBkGnd();
  322.         break;
  323.  
  324.     case IDM_EXIT:
  325.         PostMessage(hWnd, WM_CLOSE, 0, 0l);
  326.         break;
  327.  
  328.     case IDM_HELPCONTENTS:
  329.         Help(hWnd, wParam);
  330.         break;
  331.  
  332.     case IDM_ABOUT:
  333.         About(hWnd);
  334.         break;
  335.  
  336. #ifdef DEBUG
  337.     case IDM_DEBUG0:
  338.     case IDM_DEBUG1:
  339.     case IDM_DEBUG2:
  340.     case IDM_DEBUG3:
  341.     case IDM_DEBUG4:
  342.         SetDebugLevel(wParam - IDM_DEBUG0);
  343.         break;
  344. #endif
  345.  
  346.     default:
  347.         break;
  348.     }
  349. }
  350.  
  351. //
  352. // Process WM_DRAWITEM and WM_MEASUREITEM messages
  353. //
  354.  
  355. static void MeasureItem(HWND hWnd, UINT uiCtl, LPMEASUREITEMSTRUCT lpMI)
  356. {
  357.     switch (uiCtl) {
  358.  
  359.     default:
  360.  
  361.         //
  362.         // Return the height of the system font
  363.         //
  364.  
  365.         lpMI->itemHeight = tmSysFont.tmHeight;
  366.         break;
  367.     }
  368. }
  369.  
  370. static void DrawItem(HWND hWnd, UINT uiCtl, LPDRAWITEMSTRUCT lpDI)
  371. {
  372.     switch (uiCtl) {
  373.  
  374.     default:
  375.         break;
  376.     }
  377. }
  378.  
  379. //
  380. // Process WM_INITMENUPOPUP messages
  381. //
  382.  
  383. static void InitMenuPopup(HMENU hmenuPopup, UINT uiIndex, BOOL bSystem)
  384. {
  385.     CheckMenuItem(hmenuPopup,
  386.                   IDM_SHOWUPDATES,
  387.                   bShowUpdateRects ? MF_CHECKED : MF_UNCHECKED);
  388.  
  389.     CheckMenuItem(hmenuPopup,
  390.                   IDM_AUTOUPDATE,
  391.                   bAutoUpdate ? MF_CHECKED : MF_UNCHECKED);
  392.  
  393.     EnableMenuItem(hmenuPopup,
  394.                    IDM_LOADSPRITE,
  395.                    pdibBkGnd ? MF_ENABLED : MF_DISABLED | MF_GRAYED);
  396. }
  397.  
  398. //
  399. // Show a message box
  400. //
  401.  
  402. UINT cdecl Message(UINT uiBtns, LPSTR lpFormat, ...) 
  403. {
  404.     char buf[256];
  405.  
  406.     wvsprintf(buf, lpFormat, (LPSTR)(&lpFormat+1));
  407.     MessageBeep(uiBtns ? uiBtns : MB_ICONEXCLAMATION);
  408.     return (UINT) MessageBox(hwndMain, 
  409.                              buf, 
  410.                              szAppName, 
  411.                              uiBtns ? uiBtns : MB_OK|MB_ICONEXCLAMATION);
  412. }
  413.  
  414.